package com.zerowidth.moc.web.controller;

import com.zerowidth.moc.web.auth.annotation.NeedToken;
import com.zerowidth.moc.web.controller.service.AlbumRecordService;
import com.zerowidth.moc.web.resp.ResultBean;
import com.zerowidth.db.bean.AlbumRecordBean;
import com.zerowidth.db.bean.vm.DaySimpleVM;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;
import java.util.List;

@RestController
@RequestMapping("/api/v1")
public class AlbumRecordController {

    @Autowired
    AlbumRecordService albumRecordService;

    @NeedToken
    @PostMapping("/albumRecord")
    public ResponseEntity<ResultBean<AlbumRecordBean>> createAlbumRecord(
            @RequestBody AlbumRecordBean albumRecordBean) {

        return albumRecordService.createAlbumRecord(albumRecordBean);
    }

    @NeedToken
    @GetMapping("/albumRecordList")
    public ResponseEntity<ResultBean<Collection<DaySimpleVM>>> getAlbumPageList(
            @RequestParam("user_id") String user_id,
            @RequestParam("baby_id") String baby_id,
            @RequestParam(value = "day", required = false) String day,
            @RequestParam(value = "count", required = false) int count) {

        long s = System.currentTimeMillis();
        ResponseEntity<ResultBean<Collection<DaySimpleVM>>> res = albumRecordService.getAlbumPageList(
                user_id, baby_id, day, count);
        long e = System.currentTimeMillis();

        System.out.println("获取时光轴分页数据耗时=" + (e - s));
        return res;
    }

    @NeedToken
    @GetMapping("/dayRecordList")
    public ResponseEntity<ResultBean<List<AlbumRecordBean>>> getDayRecordList(
            @RequestParam("baby_id") String baby_id,
            @RequestParam("day") String day,
            @RequestParam(value = "record_id", required = false) String record_id,
            @RequestParam(value = "count", required = false) int count) {

        if (count == 0) {
            count = 12;
        }

        ResponseEntity<ResultBean<List<AlbumRecordBean>>> res = albumRecordService.getDayRecordList(
                baby_id, day, record_id, count);

        return res;
    }


    @NeedToken
    @DeleteMapping("/albumRecord")
    public ResponseEntity<ResultBean<Void>> deleteAlbumRecord(
            @RequestParam("id") String id) {

        ResponseEntity<ResultBean<Void>> res = albumRecordService.deleteAlbumRecord(id);

        return res;
    }

}
